package com.jianyun.jyzs.base;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.LoginActivity;
import com.jianyun.jyzs.utils.Const;
import com.jianyun.jyzs.widget.PromptDialog;

import butterknife.Unbinder;
import cn.rongcloud.rce.lib.AuthTask;
import cn.rongcloud.rce.lib.RceErrorCode;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by yangshijun on 2018/5/23.
 */

public abstract class MVPBaseActivity<V extends MvpView, P extends MvpPresenter<V>>
        extends MvpActivity<V, P> implements MvpView, AuthTask.LoginStateObserver {

    ImageButton backImgBtn;
    TextView titleTextView;
    TextView subTitleTextView;
    ViewGroup titleLinearLayout;
    ImageButton optionImgBtn;
    TextView tvMidTitle;
    ViewGroup actionBarContainer;
    ViewGroup contentLinearLayout;


    private Unbinder bind;
    private ActionBar actionBar;
    protected final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.rce_activity_base);

        actionBarContainer = (ViewGroup) findViewById(R.id.rl_actionbar);
        backImgBtn = (ImageButton) findViewById(R.id.imgbtn_nav_back);
        optionImgBtn = (ImageButton) findViewById(R.id.imgbtn_nav_option);
        titleTextView = (TextView) findViewById(R.id.tv_nav_title);
        subTitleTextView = (TextView) findViewById(R.id.tv_nav_sub_title);
        titleLinearLayout = (LinearLayout) findViewById(R.id.ll_nav_title);
        contentLinearLayout = (LinearLayout) findViewById(R.id.ll_content);
        tvMidTitle = (TextView) findViewById(R.id.tv_nav_mid_title);

        backImgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClick();
            }
        });
        optionImgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionClick();
            }
        });
        onCreateActionBar(new ActionBar());


    }

    public void hintActionBar(boolean isHint){
        if (isHint) actionBarContainer.setVisibility(View.GONE);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);

    }

    @Override
    public void setContentView(View view) {
        contentLinearLayout.addView(view);

    }



    /**
     * 如果需要自定义 actionbar，重写此方法。
     * 根据 {@link ActionBar#setTitle(String)}
     * {@link ActionBar#setBackDrawable(Drawable)}
     * {@link ActionBar#setOptionDrawable(Drawable)}
     * 方法设置 actionbar 中对应的各个组件。
     *
     * @param actionBar actionbar 容器
     */
    public void onCreateActionBar(ActionBar actionBar) {
        this.actionBar = actionBar;
    }

    public void onBackClick() {
        finish();
    }

    public void onOptionClick() {
        // override
    }

    @Override
    public void onLoginSuccess() {

    }

    @Override
    public void onLoginFailure(RceErrorCode errorCode) {

    }

    @Override
    public void onInactive() {
        RceLog.w(TAG, "onInactive");
        PromptDialog dialog = PromptDialog.newInstance(this, getString(R.string.rce_user_disabled), "");
        dialog.setPromptButtonClickedListener(new PromptDialog.OnPromptButtonClickedListener() {
            @Override
            public void onPositiveButtonClicked() {
                Intent intent = new Intent(MVPBaseActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

            @Override
            public void onNegativeButtonClicked() {

            }
        });
        dialog.disableCancel();
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    public void onReLogin() {
        RceLog.i(TAG, "onReLogin");
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra(Const.RE_LOGIN, true);
        startActivity(intent);
        finish();
    }

    @Override
    public void onConnectSuccess() {

    }

    //取消订阅 、绑定
    @Override
    protected void onDestroy() {

        super.onDestroy();
    }


    public class ActionBar {
        public void setBackDrawable(Drawable drawable) {
            backImgBtn.setVisibility(View.VISIBLE);
            backImgBtn.setImageDrawable(drawable);
        }

        public void setOptionDrawable(Drawable drawable) {
            optionImgBtn.setVisibility(View.VISIBLE);
            optionImgBtn.setImageDrawable(drawable);
        }

        public void setOptionVisible(int visible) {
            optionImgBtn.setVisibility(visible);
        }

        public void setTitle(String title) {
            titleTextView.setVisibility(View.VISIBLE);
            titleTextView.setText(title);
        }

        public void setTitle(@StringRes int resId) {
            String title = getString(resId);
            setTitle(title);
        }

        public void setSubTitle(String subTitle) {
            if (!TextUtils.isEmpty(subTitle)) {
                subTitleTextView.setVisibility(View.VISIBLE);
                subTitleTextView.setText(subTitle);
                subTitleTextView.setTextColor(getResources().getColor(R.color.color_gray_text));
            } else {
                subTitleTextView.setVisibility(View.GONE);
            }
        }

        public void setActionBarVisibility(int visibility) {
            actionBarContainer.setVisibility(visibility);
        }


        public void setMiddleTitle(String title) {
            if (!TextUtils.isEmpty(title)) {
                tvMidTitle.setVisibility(View.VISIBLE);
                tvMidTitle.setText(title);
            } else {
                tvMidTitle.setVisibility(View.GONE);
            }
        }

        /**
         * 设置自定义 actionbar，不再使用 Base 中的样式。
         *
         * @param resId 布局文件
         * @return 填充后的 View Container
         */
        public View setActionBar(int resId) {
            backImgBtn.setVisibility(View.GONE);
            optionImgBtn.setVisibility(View.GONE);
            titleLinearLayout.setVisibility(View.GONE);
            return LayoutInflater.from(MVPBaseActivity.this).inflate(resId, actionBarContainer);
        }
    }
}
